package com.gable.report.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.util.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * Created by mj on 2018/1/17.
 */
public class MessageUtil {
    //将前端传过来的字符串转换成html字符串
    public static String getHtml(String clientName,String createTime,String jsonStr){
        if(StringUtils.isEmpty(jsonStr)){
            return "";
        }
        String sendTime = createTime.substring(createTime.indexOf("-")+1,createTime.length());
        sendTime = sendTime.substring(0, sendTime.lastIndexOf(":"));
        String html = "<div style='position: relative'><div style='position: absolute;#VuserV#: 0;top: 2px'><img src='../images/user.png' style='width: 45px'></div><p style='color: #333;text-align:#VstyleV#;#VpaddingV#' class='name-p'><span style='margin-right: 10px;displayRight'>"+sendTime+"</span>"+clientName+"<span style='margin-left: 10px;displayLeft'>"+sendTime+"</span></p> </div>";
        JSONArray array = JSON.parseArray(jsonStr);
        for(int i = 0;i<array.size();i++){
            if(i == 0)
                html +="<p style='text-align:#VstyleV#;#VpaddingV#'><label style='padding-left:10px;padding-right: 10px;#VcolorV#' class='text-p'>";
            Object obj = array.get(i);
            boolean b = obj instanceof JSONObject;
            if(!b){
                html +=obj;
                if(i+1 == array.size()){
                    html +="<br></label></p>";
                }
                continue;
            }
            JSONObject jsonObj = (JSONObject) array.get(i);
            Object objTag = jsonObj.get("tag");
            if("img".equals(objTag)){
                JSONArray attrsArray = (JSONArray) jsonObj.get("attrs");
                String img = "<img src = '#S#' alt = '#A#' data-w-e = '#D#' style= '#Y#'>";
                String src = "";
                String alt = "";
                String datawe="";
                String style = "";
                for(int k = 0;k<attrsArray.size();k++){
                    JSONObject attrObj = (JSONObject) attrsArray.get(k);
                    if("src".equals(attrObj.get("name").toString())){
                        src = attrObj.get("value").toString();
                    }else if("alt".equals(attrObj.get("name").toString())){
                        alt = attrObj.get("value").toString();
                    }else if("data-w-e".equals(attrObj.get("name").toString())){
                        datawe = attrObj.get("value").toString();
                    }else if("style".equals(attrObj.get("name").toString())){
                        style = attrObj.get("value").toString();
                    }
                }
                img = img.replace("#S#",src);
                img = img.replace("#A#",alt);
                img = img.replace("#D#",datawe);
                img = img.replace("#Y#",style);
                html +=img;
                if(i+1 == array.size()){
                    html +="<br></label></p>";
                }
            }else{
                Object children = jsonObj.get("children");
                JSONArray childrenArray = JSONArray.parseArray(children.toString());
//                html +="<p style='text-align:#VstyleV#;#VpaddingV#'><label style='padding-left:10px;padding-right: 10px;#VcolorV#' class='text-p'>";
                for(int j = 0;j<childrenArray.size();j++){
                    Object childrenObj = childrenArray.get(j);
                    if(childrenObj instanceof JSONObject){
                        JSONObject childrenJson = (JSONObject) childrenObj;
                        String tag = childrenJson.get("tag").toString();
                        if(!StringUtils.isEmpty(tag)){
                            if("img".equals(tag)){
                                JSONArray attrsArray = (JSONArray) childrenJson.get("attrs");
                                String img = "<img src = '#S#' alt = '#A#' data-w-e = '#D#' style= '#Y#'>";
                                String src = "";
                                String alt = "";
                                String datawe="";
                                String style = "";
                                for(int k = 0;k<attrsArray.size();k++){
                                    JSONObject attrObj = (JSONObject) attrsArray.get(k);
                                    if("src".equals(attrObj.get("name").toString())){
                                        src = attrObj.get("value").toString();
                                    }else if("alt".equals(attrObj.get("name").toString())){
                                        alt = attrObj.get("value").toString();
                                    }else if("data-w-e".equals(attrObj.get("name").toString())){
                                        datawe = attrObj.get("value").toString();
                                    }else if("style".equals(attrObj.get("name").toString())){
                                        style = attrObj.get("value").toString();
                                    }
                                }
                                img = img.replace("#S#",src);
                                img = img.replace("#A#",alt);
                                img = img.replace("#D#",datawe);
                                img = img.replace("#Y#",style);
                                html +=img;
                            }else if("br".equals(tag)){
                                html += "<br>";
                            }
                        }
                        if(childrenArray.size() == (j+1)){
                            html+= "</label></p>";
                        }
                    }else if(childrenArray.size() == 1){
                        html +=childrenObj+"</label></p>";
                    }else{
                        html +=childrenObj;
                    }
                }
            }
        }
        return html;
    }

    //清理消息内容 去掉html标签，把回车换行等符号去掉，首尾空格
    public static String cleanContent(String htmlStr){
        String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
        String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
        String regEx_img = "<img[^>]+>"; // 定义HTML标签的正则表达式
        String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
        //String regEx_space = "\\s*|\t|\r|\n";//定义空格回车换行符

        Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
        Matcher m_script = p_script.matcher(htmlStr);
        htmlStr = m_script.replaceAll(""); // 过滤script标签

        Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
        Matcher m_style = p_style.matcher(htmlStr);
        htmlStr = m_style.replaceAll(""); // 过滤style标签

        Pattern p_img = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
        Matcher m_img = p_img.matcher(htmlStr);
        htmlStr = m_img.replaceAll("[图片]"); // 过滤html标签

        Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
        Matcher m_html = p_html.matcher(htmlStr);
        htmlStr = m_html.replaceAll(""); // 过滤html标签

        /*Pattern p_space = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE);
        Matcher m_space = p_space.matcher(htmlStr);
        htmlStr = m_space.replaceAll(""); // 过滤空格回车标签*/

        htmlStr = StringUtils.replace(htmlStr,"&nbsp;"," ");
        return htmlStr;
    }

    // 截取正真的内容
    //<p style='color: #333' class='name-p'>maojun1<span style='margin-left: 10px'>2018-01-25 11:09:12</span></p><label style='padding-left:10px;padding-right: 10px' class='text-p'>嗯 我是谷歌</label><br>
    //嗯 我是谷歌
    public static String splitNotNumber(String content) {
        String string = "管理员:[图片]03-2017:21管理员03-2017:21赶快来修";
        int i = content.lastIndexOf(":");
        String str = content.substring(i+3,content.length());
        return str;
    }
}
